﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public partial class TQPSTEP : ViewBase
    {
        #region ==QPLAN===========
        public static readonly DependencyProperty QPLANProperty = DependencyProperty.Register("QPLAN",
            typeof(string), typeof(TQPSTEP),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQPLANChanged));
        public string QPLAN
        {
            get
            {
                return (string)GetValue(QPLANProperty);
            }
            set
            {
                SetValue(QPLANProperty, value);
                ((VQPSTEP)MV).QPLAN = value;
                LOADQPS();
            }
        }
        private static void OnQPLANChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQPSTEP;
            if (src != null)
            {
                src.QPLAN = (string)e.NewValue;
            }
        }
        #endregion

         #region ==MERKM===========
        public static readonly DependencyProperty MERKMProperty = DependencyProperty.Register("MERKM",
            typeof(string), typeof(TQPSTEP),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMERKMChanged));
        public string MERKM
        {
            get
            {
                return (string)GetValue(MERKMProperty);
            }
            set
            {
                SetValue(MERKMProperty, value); 
             }
        }
        private static void OnMERKMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQPSTEP;
            if (src != null)
            {
                src.MERKM = (string)e.NewValue;
            }
        }
        #endregion
        
        #region ==FTAUTOQC===========
        public static readonly DependencyProperty FTAUTOQCProperty = DependencyProperty.Register("FTAUTOQC",
            typeof(bool), typeof(TQPSTEP),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFTAUTOQCChanged));
        public bool FTAUTOQC
        {
            get
            {
                return (bool)GetValue(FTAUTOQCProperty);
            }
            set
            {
                SetValue(FTAUTOQCProperty, value);
                ((VQPSTEP)MV).FTAUTOQC = value;
            }
        }
        private static void OnFTAUTOQCChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQPSTEP;
            if (src != null)
            {
                src.FTAUTOQC = (bool)e.NewValue;
            }
        }
        #endregion

    
        #region ==dwQT===========
        public static readonly DependencyProperty dwQTProperty = DependencyProperty.Register("dwQT",
            typeof(DataRow), typeof(TQPSTEP),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwQTChanged));
        public DataRow dwQT
        {
            get
            {
                return (DataRow)GetValue(dwQTProperty);
            }
            set
            {
                SetValue(dwQTProperty, value);
                if (dwQT == null)
                    return;
                if (IsEdit && dwQPS != null)
                {
                    dwQPS["QPSMERKM"] = dwQT["QPMMERKM"];
                    dwQPS["QPSNSWERT"] = dwQT["QPMVERS"];
                    dwQPS["QPSNSUG"] = dwQT["QPMWERTV"];
                    dwQPS["QPSNSOG"] = dwQT["QPMWERTB"];
                }
            }
        }
        private static void OdwQTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQPSTEP;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwQT = row;
            }
        }
        #endregion

        #region ==dwQPS===========
        public static readonly DependencyProperty dwQPSProperty = DependencyProperty.Register("dwQPS",
            typeof(DataRow), typeof(TQPSTEP),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwQPSChanged));
        public DataRow dwQPS
        {
            get
            {
                return (DataRow)GetValue(dwQPSProperty);
            }
            set
            {
                SetValue(dwQPSProperty, value);
            }
        }
        private static void OdwQPSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQPSTEP;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwQPS = row;
            }
        }
        #endregion

        public TQPSTEP()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VQPSTEP();
            MESSAGES += ";";
            this.DataContext = MV;
            InitializeComponent();
            IsEdit = false;
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwQPS = dwMaster;
            if (dwQPS == null)
            {
                MERKM = "";
            }
            else
            {
                MERKM = dwQPS["QPSMERKM"].ToString();
            }
        }

        public override void OnWorkflowIDChange(ReflectionStudio.Core.Events.MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;

        }

        public void LOADQPS()
        {
            if (QPLAN.Length <8)
            {
                TBMain.Clear();
                return;
            }
            ((VQPSTEP)MV).LINE = 0;
            KRS = QPLAN.Substring(0, 2);
            LFD = QPLAN.Substring(2, 6);
            dwData = TBMain.NewRow();
            dwData["QPSMANDANT"] = app.Mandant;
            dwData["QPSPNRKRS"] = KRS;
            dwData["QPSPLFDNR"] = LFD;
            SResult = ((VQPSTEP)MV).QueryQPSTEP(dwData, 1);
            MergeData(SResult, true);
            if (FTAUTOQC)
            {
                if (TBMain.Rows.Count == 0)
                {
                    ((VQPSTEP)MV).NEWQPS();
                }
            }
            vwMaster.View.MoveCurrentToFirst();
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (dwQPS == null)
                return;
            ((VQPSTEP)MV).COPYQPS(dwQPS);
            vwMaster.View.MoveCurrentToFirst();
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwQPS == null)
                return;
            foreach (DataRow row in TBMain.Rows)
            {
                row["QPSAENB"] = app.User;
                row["QPSAEND"] = vm.date;   
                ((VQPSTEP)MV).UpdateQPSTEP(row, app.company, 0);
            }
            base.SaveCommand(sender, e);
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwQPS == null)
                return;
            ((VQPSTEP)MV).DeleteQPSTEP(dwQPS, 1);  
            filter = "QPSPNRKRS='" + dwQPS["QPSPNRKRS"].ToString() + "' AND QPSPLFDNR='" + dwQPS["QPSPLFDNR"].ToString() + "' AND QPSSTEP='" + dwQPS["QPSSTEP"].ToString()+"'";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            vwMaster.View.MoveCurrentToFirst();
            base.DeleteCommand(sender, e);
        }

        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            if (dwQPS == null)
                return;
            ((VQPSTEP)MV).COPYQPS(dwQPS);
            vwMaster.View.MoveCurrentToFirst();
            base.CopyCommand(sender, e);
        }
        public void BatchSave()
        {
            SaveCommand(null, null);
        }
    }
}
